package comp.wxapi.utils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import comp.wxapi.vo.RouteConfig;
import comp.wxapi.vo.WXConfig;

public class ConfigUtils {
	
	public static final String GET_CONFIG_LIKE_KEY_URL = "/config/config/configs/search/like";
	
	public static final String GET_CONFIG_EQUAL_KEY_URL = "/config/config/search";
	
	public static final String GET_CONFIG_EQUAL_APPID_URL = "/config/config/configs/search/appid/";
	
	private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);
	
	private ConfigUtils() {}
	
	public static String getGatewayUrl() {
		
		return "http://"+System.getenv("GATEWAY_HOST");
	}
	
	/**
	 * 根据key模糊查询配置信息
	 * @param key
	 * @param group
	 * @return
	 */
	public static List<WXConfig> getConfigsLikeKey(String key,String group) {
		Map<String,String> param = new HashMap<String,String>();
		param.put("key", key);
		param.put("group", group);
		String res = null;
		try {
			res = RequestUtils.getWxconfig(getGatewayUrl()+GET_CONFIG_LIKE_KEY_URL, param).string();
			return JSONObject.parseArray(res, WXConfig.class);
		} catch (IOException e) {
			logger.error("get configs like key fail",e);
		}
		return Collections.emptyList();
	}
	
	/**
	 * 根据key精确查询配置信息
	 * @param key
	 * @param group
	 * @return
	 */
	public static WXConfig getConfigByKey(String key,String group) {
		Map<String,String> param = new HashMap<String,String>();
		param.put("key", key);
		param.put("group", group);
		String res = null;
		try {
			res = RequestUtils.getWxconfig(getGatewayUrl()+GET_CONFIG_EQUAL_KEY_URL, param).string();
			return JSONObject.parseObject(res, WXConfig.class);
		} catch (IOException e) {
			logger.error("get configs like key fail",e);
		}
		return new WXConfig();
	}
	
	/**
	 * 根据appid获取配置信息
	 * @param appid
	 * @return
	 */
	public static List<WXConfig> getConfigsByAppid(String appid) {
		String res = null;
		try {
			res = RequestUtils.getWxconfig(getGatewayUrl()+GET_CONFIG_EQUAL_APPID_URL+appid, null).string();
			return JSONObject.parseArray(res, WXConfig.class);
		} catch (IOException e) {
			logger.error("get configs by appid fail",e);
		}
		return Collections.emptyList();
	}
	
	/**
	 * 根据key获取注册的路由信息
	 * @param key
	 * @param group
	 * @return
	 */
	public static List<RouteConfig> getRouteConfig(String key,String group) {
		WXConfig config = getConfigByKey(key, group);
		if(config != null) {
			
			return JSONObject.parseArray(config.getFvalue(), RouteConfig.class);
		}
		return Collections.emptyList();
	}
	
//	public static List<WXConfig> getTestDefaultConfig() {
//		List<WXConfig> configs = new ArrayList<>();
//		WXConfig appid = new WXConfig();
//		appid.setFkey(Constants.APPID_KEY);
//		appid.setFvalue("wx61458dc98c75b7d0");
//		configs.add(appid);
//		WXConfig secret = new WXConfig();
//		secret.setFkey(Constants.APP_SECRET_KEY);
//		secret.setFvalue("91d86e2159f3213068c7ce76b40e919b");
//		configs.add(secret);
//		WXConfig mchId = new WXConfig();
//		mchId.setFkey(Constants.PAY_MCHID_KEY);
//		mchId.setFvalue("1305137601");
//		configs.add(mchId);
//		WXConfig mchKey = new WXConfig();
//		mchKey.setFkey(Constants.PAY_MCHKEY_KEY);
//		mchKey.setFvalue("ZhA5BSnlMqesOyC6A5BqnXZ57mVLM8wb");
//		configs.add(mchKey);
//		return configs;
//	}
	
}
